﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaiwuModFramework.Common.Utils {
    public class PathUtils {
        private PathUtils() { }

        // 太吾绘卷应用文件夹
        public static readonly string TAIWU_DIR = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory);
        public static readonly string TAIWU_FILE_NAME = "The Scroll Of Taiwu";

        static PathUtils() {
            if(TAIWU_DIR.Contains(TAIWU_FILE_NAME)) {
                var paths = TAIWU_DIR.Split('/', '\\');
                StringBuilder sb = new StringBuilder();
                foreach(var p in paths) {
                    sb.Append(p).Append("/");
                    if(TAIWU_FILE_NAME.Equals(p)) {
                        break;
                    }
                }
                TAIWU_DIR = Path.GetFullPath(sb.ToString());
            }

            //TAIWU_DIR.Split("The Scroll Of Taiwu");
        }

        /// <summary>
        /// 格式化文件路
        /// 若为空，则返回程序所在路径
        /// 若为相对路径，则转为绝对路径
        /// 若为绝对路径，则进行格式化
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string FormatFilePath(string filePath) {
            if(string.IsNullOrEmpty(filePath)) {
                filePath = TAIWU_DIR;
            } else if(!Path.IsPathRooted(filePath)) { // 是相对路径
                filePath = Path.GetFullPath(TAIWU_DIR + filePath);
            } else {
                filePath = Path.GetFullPath(filePath);
            }
            return filePath;
        }
    }
}
